package com.zrwl.poscloud.xo.service.impl;

import com.zrwl.poscloud.base.constants.HomeworkBatchConstants;
import com.zrwl.poscloud.base.entity.GradeTeacherR;
import com.zrwl.poscloud.base.entity.HomeworkBatch;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.entity.SysDictData;
import com.zrwl.poscloud.base.enums.EMessageModel;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.enums.MessageModelType;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.DateUtils;
import com.zrwl.poscloud.commons.utils.JsonUtils;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.mapper.EmployeeMapper;
import com.zrwl.poscloud.xo.mapper.HomeworkDetailMapper;
import com.zrwl.poscloud.xo.service.GradeTeacherRService;
import com.zrwl.poscloud.xo.service.MessageModelService;
import com.zrwl.poscloud.xo.service.SysDictDataService;
import com.zrwl.poscloud.xo.vo.EmployeeVO;
import com.zrwl.poscloud.xo.vo.HomeworkDetailVO;
import com.zrwl.poscloud.xo.vo.MessageModelVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.zrwl.poscloud.xo.mapper.HomeworkBatchMapper;
import com.zrwl.poscloud.xo.service.HomeworkBatchService;
import com.zrwl.poscloud.xo.vo.HomeworkBatchVO;

import javax.annotation.Resource;

/**
 * 作业管理 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-03-16
 */
@Service
public class HomeworkBatchServiceImpl extends SuperServiceImpl<HomeworkBatchMapper, HomeworkBatch> implements HomeworkBatchService {

    @Autowired
    private HomeworkBatchService homeworkBatchService;
    @Resource
    private HomeworkBatchMapper homeworkBatchMapper;
    @Autowired
    private GradeTeacherRService gradeTeacherRService;
    @Resource
    private HomeworkDetailMapper homeworkDetailMapper;
    @Autowired
    private MessageModelService messageModelService;
    @Autowired
    private SysDictDataService sysDictDataService;
    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public IPage<HomeworkBatchVO> getPageList(HomeworkBatchVO homeworkBatchVO) {
        Page<HomeworkBatch> page = new Page<>();
        page.setCurrent(homeworkBatchVO.getCurrentPage());
        page.setSize(homeworkBatchVO.getPageSize());
        IPage<HomeworkBatchVO> pageList = homeworkBatchMapper.getPageList(page, homeworkBatchVO);
        List<HomeworkBatchVO> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public List<HomeworkBatch> getAllList(HomeworkBatchVO homeworkBatchVO) {
        QueryWrapper<HomeworkBatch> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        List<HomeworkBatch> list = homeworkBatchService.list(queryWrapper);
        return list;
    }

    @Override
    public String add(HomeworkBatchVO homeworkBatchVO) {
        HomeworkBatch entity = new HomeworkBatch();
        BeanUtils.copyProperties(homeworkBatchVO, entity, SysConf.STATUS);
        entity.insert();
        homeworkBatchVO.setUid(entity.getUid());
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String edit(HomeworkBatchVO homeworkBatchVO) {
        HomeworkBatch entity = homeworkBatchService.getById(homeworkBatchVO.getUid());
        BeanUtils.copyProperties(homeworkBatchVO, entity);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String delete(HomeworkBatchVO homeworkBatchVO) {
        HomeworkBatch entity = homeworkBatchService.getById(homeworkBatchVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    /**
     * 作业发布发送作业提醒
     *
     * @param homeworkBatchVO
     * @return
     */
    @Override
    public String editWorkRelease(HomeworkBatchVO homeworkBatchVO) {
        HomeworkBatch entity = homeworkBatchService.getById(homeworkBatchVO.getUid());
        if (entity.getStatus() == HomeworkBatchConstants.release) {
            throw new RuntimeException("该作业处于发布状态");
        }
        GradeTeacherR gradeTeacherR = gradeTeacherRService.getGradeTeacherR(homeworkBatchVO.getTeacherUid(), homeworkBatchVO.getSubjectType());
        if (gradeTeacherR == null || gradeTeacherR.getTaskPublish() == 0) {
            throw new RuntimeException("该教师未授权作业发布");
        }
        entity.setStatus(HomeworkBatchConstants.release);
        entity.setPublishTime(new Date());
        //发送微信消息-作业发布通知
        List<HomeworkDetailVO> voList;
        MessageModelVO messageModelVO = new MessageModelVO();
        //判断作业是否已发布
        if (entity.getStatus() == HomeworkBatchConstants.release) {
            homeworkBatchVO.setStatus(entity.getStatus());
            homeworkBatchVO.setPublishTime(entity.getPublishTime());
            //作业类型为指定人员
            if (entity.getType() == HomeworkBatchConstants.type_employee) {
                voList = homeworkDetailMapper.getHomeworkDetailByBatchUid(homeworkBatchVO.getUid());
                if (voList.size() > 0) {
                    for (HomeworkDetailVO homeworkDetailVO : voList) {
                        //查询学生姓名
                        EmployeeVO employee = employeeMapper.getEmployeeByUid(homeworkDetailVO.getStudentUid());
                        homeworkBatchVO.setStudentName(employee.getName());
                        homeworkBatchVO.setStatusName("已发布");
                        //判断手机用户接收人是否为空
                        if (StringUtils.isNotBlank(homeworkDetailVO.getAcceptUserUid())) {
                            messageModelVO.setToUid(homeworkDetailVO.getAcceptUserUid());

                        } else {
                            messageModelVO.setToUid(homeworkDetailVO.getStudentUid());
                        }
                        sendHomeworkMessage(messageModelVO, EMessageModel.PLATFORM_TYPE_WX, MessageModelType.WX_HOME_WORK_NOTICE, homeworkBatchVO);
                    }
                }
            }
            //作业类型为全科
            if (homeworkBatchVO.getType() == HomeworkBatchConstants.type_all_course) {
                voList = homeworkDetailMapper.getHomeworkDetailByBatchUid(homeworkBatchVO.getUid());
                if (voList.size() > 0) {
                    for (HomeworkDetailVO homeworkDetailVO : voList) {
                        //查询学生姓名
                        EmployeeVO employee = employeeMapper.getEmployeeByUid(homeworkDetailVO.getStudentUid());
                        homeworkBatchVO.setStudentName(employee.getName());
                        homeworkBatchVO.setStatusName("已发布");
                        //判断手机用户接收人是否为空
                        if (StringUtils.isNotBlank(homeworkDetailVO.getAcceptUserUid())) {
                            messageModelVO.setToUid(homeworkDetailVO.getAcceptUserUid());
                        } else {
                            messageModelVO.setToUid(homeworkDetailVO.getStudentUid());
                        }
                        sendHomeworkMessage(messageModelVO, EMessageModel.PLATFORM_TYPE_WX, MessageModelType.WX_HOME_WORK_NOTICE, homeworkBatchVO);
                    }
                }
            }
        }
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    /**
     * 作业撤回发送作业撤回提醒
     *
     * @param homeworkBatchVO
     * @return
     */
    @Override
    public String editWorkRepeal(HomeworkBatchVO homeworkBatchVO) {
        HomeworkBatch entity = homeworkBatchService.getById(homeworkBatchVO.getUid());
        if (entity.getStatus() == HomeworkBatchConstants.not_release) {
            throw new RuntimeException("该作业处于未发布状态");
        }
        entity.setCancelReason(homeworkBatchVO.getCancelReason());
        entity.setStatus(HomeworkBatchConstants.not_release);
        //发送微信消息-作业撤销通知
        List<HomeworkDetailVO> voList = new ArrayList<>();
        MessageModelVO messageModelVO = new MessageModelVO();
        //作业类型指定人员
        if (homeworkBatchVO.getType() == HomeworkBatchConstants.type_employee) {
            voList = homeworkDetailMapper.getHomeworkDetailByBatchUid(homeworkBatchVO.getUid());
            if (voList.size() > 0) {
                for (HomeworkDetailVO homeworkDetailVO : voList) {
                    //查询学生姓名
                    EmployeeVO employee = employeeMapper.getEmployeeByUid(homeworkDetailVO.getStudentUid());
                    homeworkBatchVO.setStudentName(employee.getName());
                    homeworkBatchVO.setStatusName("已撤销");
                    //判断手机用户接收人是否为空
                    if (StringUtils.isNotBlank(homeworkDetailVO.getAcceptUserUid())) {
                        messageModelVO.setToUid(homeworkDetailVO.getAcceptUserUid());
                    } else {
                        messageModelVO.setToUid(homeworkDetailVO.getStudentUid());
                    }
                    sendHomeworkMessage(messageModelVO, EMessageModel.PLATFORM_TYPE_WX, MessageModelType.WX_HOME_WORK_NOTICE, homeworkBatchVO);
                }
            }
        }
        //作业类型为全科
        if (homeworkBatchVO.getType() == HomeworkBatchConstants.type_all_course) {
            voList = homeworkDetailMapper.getHomeworkDetailByBatchUid(homeworkBatchVO.getUid());
            if (voList.size() > 0) {
                for (HomeworkDetailVO homeworkDetailVO : voList) {
                    //查询学生姓名
                    EmployeeVO employee = employeeMapper.getEmployeeByUid(homeworkDetailVO.getStudentUid());
                    homeworkBatchVO.setStudentName(employee.getName());
                    homeworkBatchVO.setStatusName("已撤销");
                    //判断手机用户接收人是否为空
                    if (StringUtils.isNotBlank(homeworkDetailVO.getAcceptUserUid())) {
                        messageModelVO.setToUid(homeworkDetailVO.getAcceptUserUid());
                    } else {
                        messageModelVO.setToUid(homeworkDetailVO.getStudentUid());
                    }
                    sendHomeworkMessage(messageModelVO, EMessageModel.PLATFORM_TYPE_WX, MessageModelType.WX_HOME_WORK_NOTICE, homeworkBatchVO);
                }
            }
        }
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    /**
     * 发送作业消息提醒
     *
     * @param messageModelVO
     * @param homeworkBatchVO
     * @return
     */
    public String sendHomeworkMessage(MessageModelVO messageModelVO, int platformType, MessageModelType messageModelType, HomeworkBatchVO homeworkBatchVO) {
        messageModelVO.setCompanyUid(homeworkBatchVO.getCompanyUid());
        messageModelVO.setPlatformType(platformType);
        messageModelVO.setType(messageModelType.getType());
        List<SysDictData> subjectType = JsonUtils.jsonToList(JsonUtils.objectToJson(sysDictDataService.getListByDictType("subject_type").get("list")), SysDictData.class);
        if (subjectType != null) {
            for (SysDictData sysDictData : subjectType) {
                if (homeworkBatchVO.getSubjectType() == Integer.parseInt(sysDictData.getDictValue())) {
                    homeworkBatchVO.setSubjectName(sysDictData.getDictLabel());
                }
            }
        }
        //布置时间
        String publishTime = DateUtils.formatDateAMPMS(homeworkBatchVO.getPublishTime(), "yyyy年MM月dd日 HH:mm");
        String beginTime = homeworkBatchVO.getBeginTime();
        String endTime = homeworkBatchVO.getEndTime();
        //提交时间
        String submitTime = beginTime + "~" + endTime;
        String str = "【" + homeworkBatchVO.getStudentName() + "的" + homeworkBatchVO.getSubjectName() + "作业】" + homeworkBatchVO.getStatusName() + "," + submitTime + "," + homeworkBatchVO.getContent();
        String content = MessageModelType.getParams(platformType, messageModelType, str);
        messageModelVO.setContent(content);
        return messageModelService.addSendMessage(messageModelVO);
    }
}
